package com.example.hfSystem.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.example.hfSystem.annotation.IgnoreAuth;
import com.example.hfSystem.entity.pojo.Aboutus;
import com.example.hfSystem.service.AboutusService;
import com.example.hfSystem.utils.MPUtil;
import com.example.hfSystem.utils.PageUtils;
import com.example.hfSystem.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 关于我们 前端控制器
 * </p>
 *
 * @since 2024-04-09
 */
@RestController
@RequestMapping("/aboutus")
@Api(tags = "关于我们 ")
public class AboutusController {
    @Autowired
    private AboutusService aboutusService;

    /**
     * 后端分页查询
     *
     * @param params
     * @param aboutus
     * @param request
     * @return
     */
    @RequestMapping("/page")
    @ApiOperation(value = "分页查询")
    public R page(@RequestParam Map<String, Object> params, Aboutus aboutus,
                  HttpServletRequest request) {
        EntityWrapper<Aboutus> ew = new EntityWrapper<Aboutus>();

        PageUtils page = aboutusService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, aboutus), params), params));

        return R.ok().put("data", page);
    }

    /**
     * 前端列表
     */
    @IgnoreAuth
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params, Aboutus aboutus,
                  HttpServletRequest request) {
        EntityWrapper<Aboutus> ew = new EntityWrapper<Aboutus>();

        PageUtils page = aboutusService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, aboutus), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/lists")
    public R list(Aboutus aboutus) {
        EntityWrapper<Aboutus> ew = new EntityWrapper<Aboutus>();
        ew.allEq(MPUtil.allEQMapPre(aboutus, "aboutus"));
        return R.ok().put("data", aboutusService.selectListView(ew));
    }

    /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(Aboutus aboutus) {
        EntityWrapper<Aboutus> ew = new EntityWrapper<Aboutus>();
        ew.allEq(MPUtil.allEQMapPre(aboutus, "aboutus"));
        Aboutus aboutusView = aboutusService.selectView(ew);
        return R.ok("查询关于我们成功").put("data", aboutusView);
    }

    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        Aboutus aboutus = aboutusService.selectById(id);
        return R.ok().put("data", aboutus);
    }

    /**
     * 前端详情
     */
    @IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id) {
        Aboutus aboutus = aboutusService.selectById(id);
        return R.ok().put("data", aboutus);
    }


    /**
     * 后端保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody Aboutus aboutus, HttpServletRequest request) {
        aboutus.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
        aboutusService.insert(aboutus);
        return R.ok();
    }

    /**
     * 前端保存
     */
    @RequestMapping("/add")
    public R add(@RequestBody Aboutus aboutus, HttpServletRequest request) {
        aboutus.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
        //ValidatorUtils.validateEntity(aboutus);
        aboutusService.insert(aboutus);
        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    public R update(@RequestBody Aboutus aboutus, HttpServletRequest request) {
        //ValidatorUtils.validateEntity(aboutus);
        aboutusService.updateById(aboutus);//全部更新
        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        aboutusService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 提醒接口
     */
    @RequestMapping("/remind/{columnName}/{type}")
    public R remindCount(@PathVariable("columnName") String columnName, HttpServletRequest request,
                         @PathVariable("type") String type, @RequestParam Map<String, Object> map) {
        map.put("column", columnName);
        map.put("type", type);

        if (type.equals("2")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            Date remindStartDate = null;
            Date remindEndDate = null;
            if (map.get("remindstart") != null) {
                Integer remindStart = Integer.parseInt(map.get("remindstart").toString());
                c.setTime(new Date());
                c.add(Calendar.DAY_OF_MONTH, remindStart);
                remindStartDate = c.getTime();
                map.put("remindstart", sdf.format(remindStartDate));
            }
            if (map.get("remindend") != null) {
                Integer remindEnd = Integer.parseInt(map.get("remindend").toString());
                c.setTime(new Date());
                c.add(Calendar.DAY_OF_MONTH, remindEnd);
                remindEndDate = c.getTime();
                map.put("remindend", sdf.format(remindEndDate));
            }
        }

        Wrapper<Aboutus> wrapper = new EntityWrapper<Aboutus>();
        if (map.get("remindstart") != null) {
            wrapper.ge(columnName, map.get("remindstart"));
        }
        if (map.get("remindend") != null) {
            wrapper.le(columnName, map.get("remindend"));
        }


        int count = aboutusService.selectCount(wrapper);
        return R.ok().put("count", count);
    }

}
